package com.zxq.mall.realtime.util

import java.lang.reflect.{Field, Method, Modifier}
import com.zxq.mall.realtime.bean.{DauInfo,PageLog}

import scala.util.control.Breaks
object MyBeanUtils {

  /**
    * 将 srcObj 中属性的值拷贝到 destObj 对应的属性上.
    */
  def copyProperties(srcObj : AnyRef , destObj: AnyRef): Unit ={
    if(srcObj == null || destObj == null ){
      return
    }

    val srcFields: Array[Field] = srcObj.getClass.getDeclaredFields

    for (srcField <- srcFields) {
      Breaks.breakable{

        var getMethodName : String = srcField.getName

        var setMethodName : String = srcField.getName+"_$eq"

        val getMethod: Method =
          srcObj.getClass.getDeclaredMethod(getMethodName)

        val setMethod: Method =
        try{
          destObj.getClass.getDeclaredMethod(setMethodName,
            srcField.getType)
        }catch{

          case ex : Exception => Breaks.break()
        }

        val destField: Field =
          destObj.getClass.getDeclaredField(srcField.getName)
        if(destField.getModifiers.equals(Modifier.FINAL)){
          Breaks.break()
        }

        setMethod.invoke(destObj, getMethod.invoke(srcObj))
      }
    }
  }
}
